<?php

namespace Home\Controller;

use Home\Model\UserModel;
class UserController extends ExtendController {

    //显示
    public function index() {
        $user_model = M('user');
        import('ORG.Util.Page'); //导入分页类
        $count = $user_model->count(); // 查询满足要求的总记录数
        $Page = new \Think\Page($count, 10); // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show();     // 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $user_model->order('user_id DESC')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('list', $list); // 赋值数据集
        $this->assign('page', $show); // 赋值分页输出
        $this->display(); // 输出模板
    }

    //修改

    public function save() {
        $user_model = M('user');
        $data['user_id'] = $_POST['user_id'];
        $user = $user_model->where($data)->find();
        if (!empty($user)) {
            $data['user_name'] = $_POST['user_name'];
            $data['user_password'] = sha1($_POST['user_password']);
            $data['user_date'] = date('Y:m:d H:i:s');
            $result = $user_model->save($data);
            $jsonResult = array();
            if ($result != false) {
                $jsonResult["flag"] = true;
            } else {
                $jsonResult["flag"] = false;
            }
        } else {
            $jsonResult["flag"] = false;
        }

        echo json_encode($jsonResult);
    }

    //删除
    public function delete() {
        $user_model = M('user');
        $map['user_id'] = $_GET['id'];
        $result = $user_model->where($map)->delete();
        if ($result != false) {
            $this->redirect('index');
        } else {
            $this->error('删除失败!');
        }
    }

    
    
    
    
    //登录 管理
   public function checkRegister(){
        if(IS_AJAX){
           $userModel=new UserModel();
            $uid= $userModel->register($_POST['user'],$_POST['password'],$_POST['repassword'],$_POST['email'],$_POST['verify']);
                $jsonResult=array();
            if(!empty($uid) ){
                  $jsonResult["flag"] = true;  
                }else{
                   $jsonResult["flag"] = false; 
                }
                echo json_encode($jsonResult);
            }else{
            $this->error('非法访问');
            }
    }
  //验证用户名是否存在  
    public function checkUser(){
        if(IS_AJAX){
           $userModel=new UserModel(); 
           $uid=$userModel->checkField($_POST['user'], 'user');
           echo  $uid >0 ? 'true' : 'false';
        }
    }
 //验证电子邮件是否存在   
    public function checkEmail(){
        if(IS_AJAX){
           $userModel=new UserModel(); 
           $uid=$userModel->checkField($_POST['email'], 'email');
           echo  $uid >0 ? 'true' : 'false';
        }
    }
    
     //验证码验证  
    public function checkVerify(){
        if(IS_AJAX){
           $userModel=new UserModel(); 
           $uid=$userModel->checkField($_POST['verify'], 'verify');
           echo  $uid >0 ? 'true' : 'false';
        }
    }
    

}

?>